//
// Created by guanzhe on 16/1/3.
// Copyright (c) 2016 BMProject. All rights reserved.
//

#import "MsgEditVc.h"
#import "UIColor+Extension.h"
#import "UIViewController+common.h"
#import "MsgSendPerCell.h"
#import "DataModel.h"

static const NSString *cellIdentifier = @"MsgSendPerCell";

@interface MsgEditVc()<UITextViewDelegate,UITableViewDelegate, UITableViewDataSource,CommonClickDelegate>{
    NSMutableArray *_tableDataArr;

}
@property (weak, nonatomic) IBOutlet UIView *topView;
@property (weak, nonatomic) IBOutlet UIView *titleView;
@property (weak, nonatomic) IBOutlet UITextField *titleTf;
@property (weak, nonatomic) IBOutlet UITextView *contentTv;
@property (weak, nonatomic) IBOutlet UILabel *numLabel;
@property (weak, nonatomic) IBOutlet UIView *bottomView;
@property (weak, nonatomic) IBOutlet UITableView *tableView;
@property(nonatomic, assign) int page;//某些请求数据有页码的页面用到的参数

@end

@implementation MsgEditVc {

}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.topView.layer.cornerRadius = 5.0;
    self.bottomView.layer.cornerRadius = 5.0;
    self.contentTv.layer.borderWidth = 1;
    self.contentTv.layer.borderColor = [UIColor hexValue:0x999999].CGColor;
    self.titleView.layer.borderWidth = 1;
    self.titleView.layer.borderColor = [UIColor hexValue:0x999999].CGColor;
    self.contentTv.delegate = self;

    if(_type == MSG_EDIT_TYPE){
        NAV_INIT(self, @"消息修改", @"back", @selector(backButtonClick:),@"提交", @selector(rightBtnClick));
        self.contentTv.text = self.msgContent;
        self.titleTf.text = self.msgTitle;
        int num = 50 - self.msgContent.length;
        self.numLabel.text = [[@"您还可输入" stringByAppendingString:kIntToString(num)] stringByAppendingString:@"个字符"];
        [UserServices getMsgEditPushList:[[AppDelegate appDelegate] getUserInfo].token
                                   msgId:self.msgId
                         completionBlock:^(int result, id responseObject) {
                             _tableDataArr = responseObject;
                             for(MsgReceiveModel *msgReceiveModel in _tableDataArr){
                                 if([msgReceiveModel.userId isEqualToString:self.model.recUserId]){
                                     msgReceiveModel.isSelected = YES;
                                 }
                             }
                             [self.tableView reloadData];
                         }];
    }else{
        NAV_INIT(self, @"消息添加", @"back", @selector(backButtonClick:),@"提交", @selector(rightBtnClick));
        self.contentTv.text = @"";
        self.titleTf.text = @"";
        [UserServices getMsgPushList:[[AppDelegate appDelegate] getUserInfo].token completionBlock:^(int result, id responseObject) {
            _tableDataArr = responseObject;
            [self.tableView reloadData];
        }];
    }
    [self initTab];
}

- (void)initTab {
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    self.tableView.backgroundColor = [UIColor backgroundColor];
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    self.tableView.tableFooterView = [UIView new];
    self.tableView.tableFooterView.backgroundColor = [UIColor backgroundColor];
}

- (void)clickActionWithData:(JSONModel *)data {
    MsgReceiveModel *model = (MsgReceiveModel *)data;
    model.isSelected = !model.isSelected;
    [self.tableView reloadData];

}

- (void)rightBtnClick {
    if(IsStringEmpty(self.titleTf.text)){
        [SVProgressHUD showErrorWithStatus:@"标题不能为空"];
        return;
    }
    if(IsStringEmpty(self.contentTv.text)){
        [SVProgressHUD showErrorWithStatus:@"消息内容不能为空"];
        return;
    }
    NSString *userIds = @"";
    for(MsgReceiveModel *msgReceiveModel in _tableDataArr){
        if(msgReceiveModel.isSelected){
            if([userIds isEqualToString:@""]){
                userIds = msgReceiveModel.userId;
            }else{
                userIds = [userIds stringByAppendingString:[@"," stringByAppendingString:[NSString ignoreEmptyStr:msgReceiveModel.userId]]];
            }
        }
    }
    if(IsStringEmpty(userIds)){
        [SVProgressHUD showErrorWithStatus:@"请至少选择1个发送的对象"];
        return;
    }
    if(_type == MSG_EDIT_TYPE) {
        [UserServices editMsgByToken:[[AppDelegate appDelegate] getUserInfo].token
                               msgId:self.msgId
                             recUser:userIds
                             message:self.contentTv.text
                               title:self.titleTf.text
                     completionBlock:^(int result, id responseObject) {
                         [SVProgressHUD showSuccessWithStatus:@"修改成功"];
                         [self backButtonClick:nil];
                     }];
    }else{
        [UserServices newMsgByToken:[[AppDelegate appDelegate] getUserInfo].token
                            recUser:userIds
                            message:self.contentTv.text
                              title:self.titleTf.text
                    completionBlock:^(int result, id responseObject) {
                        [SVProgressHUD showSuccessWithStatus:@"添加成功"];
                        [self backButtonClick:nil];
                    }];
    }

}



#pragma mark 返回分组数

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}


#pragma mark head的高度

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    return 0;
}

#pragma mark 返回每组行数

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return _tableDataArr.count;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 30;
}

#pragma mark返回每行的单元格

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    MsgSendPerCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
    if (!cell)
        cell = [[[UINib nibWithNibName:cellIdentifier bundle:nil] instantiateWithOwner:self options:nil] lastObject];
    [cell setCellInfo:_tableDataArr andPosition:indexPath.row];
    cell.delegate = self;
    return cell;
}



- (void)textViewDidChange:(UITextView *)textView {
    int num = 50 - textView.text.length;
    self.numLabel.text = [[@"您还可输入" stringByAppendingString:kIntToString(num)] stringByAppendingString:@"个字符"];
}


@end